<?
//Created by Huynh.
//Last modified by Huynh
//On Feb 28th, 2012

//TODO: compare two decks of cards.
// I'm not sure about the rules, so I leave it.

//Note: The function printDeck is for testing purpose only.
//Delete it in the final release.

include_once("card.php");

//A deck has three card.
class Deck
{
	//The array store the information about the cards.
	private $cardArray = array();
	function __construct($a, $b, $c)
	{
		$this->cardArray[0] = $a;
		$this->cardArray[1] = $b;
		$this->cardArray[2] = $c;
		
		//Sort the array to make it simpler in the future.
		usort($this->cardArray, "cardCompare");
	}
	
	//Return the value of the deck.
	public function getValue()
	{
		$sum = 0;
		for ($i = 0; $i < 3; $i++)
		{
			$sum += $this->cardArray[$i]->getValue();
		}
		
		if ($sum % 10 == 0)
		{
			return 10;
		}else
		{
			return ($sum % 10);
		}		
	}
	
	//Compare this deck and another deck.
	//Return 1 if this deck is better, return -1 otherwise.
	public function compareTo($other)
	{
		if ($this->getValue() > $other->getValue())
		{
			return 1;
		}else if ($this->getValue() < $other->getValue())
		{
			return -1;
		}else
		{
			//the two decks have the same value. 
			//Compare the suits. 
			
			//TODO: -------------------------------------------
			//What should we do in this case?
			//Compare the value of the cards in the two suit.
			//Compare greater-value cards first.
			for ($i = 2; $i >= 0; $i--)
			{
				if ($this->cardArray[$i] > $other->cardArray[$i])
				{
					return 1;
				}else if ($this->cardArray[$i] < $other->cardArray[$i])
				{
					return -1;
				}
			}	
			
			//----------TODO -----------------------------------
			return 0; 
		}
	}//End function compareTo.
	
	//Print the cards of the deck.
	//For testing purpose. Can be deleted later.
	public function printDeck()
	{
		for ($i = 0; $i < 3; $i++)
		{
			$v = $this->cardArray[$i];
			print $this->cardArray[$i]->getValue() . " ";
			print $v->getSuit() . " ";
			print "<br/>";
		}
	}
}
?>